//
//  RenderManager.m
//  TestGameEngine
//
//  Created by Louis CHU on 20/03/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RenderManager.h"
#import "OpenGLES1Renderer.h"
static RenderManager * sSharedRenderManager;
@interface RenderManager () <OpenGLES1RendererDelegate>

@property (nonatomic, strong) NSMutableArray * renderedObjects;
@end

@implementation RenderManager
@synthesize renderedObjects;
@synthesize graphicRenderer;

+ (RenderManager *) sharedRenderManager
{
    if (!sSharedRenderManager) {
        sSharedRenderManager = [RenderManager new];
    }
    return sSharedRenderManager;
}

- (void) addRenderedObject: (id<IRender>) renderedObject_
{
    if (![self.renderedObjects containsObject:renderedObject_]) {
        [self.renderedObjects addObject:renderedObject_];
    }
}

- (void) removeRenderedObject: (id<IRender>) renderedObject_
{
    if ([self.renderedObjects containsObject:renderedObject_]) {
        [self.renderedObjects removeObject:renderedObject_];
    }
}

- (void) render
{
    [self.graphicRenderer onBeginRender];
    for(id<IRender> renderedObject in self.renderedObjects)
    {
        [self.graphicRenderer onBeginMatrixRender];
        [renderedObject render];
        [self.graphicRenderer onFinishMatrixRender];
    }
    [self.graphicRenderer onFinishRender];
}

- (NSMutableArray *) renderedObjects
{
    if (!renderedObjects) {
        renderedObjects = [NSMutableArray array];
    }
    return renderedObjects;
}


@end
